package ui.panel;

import javafx.animation.Animation;
import javafx.animation.Interpolator;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.effect.DisplacementMap;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Duration;
import org.w3c.dom.Text;

/**
 * @author reegan
 */
public class SwapNode extends Application {

    @Override
    public void start(Stage primaryStage) {
        Button button1 = new Button("start");
        button1.setLayoutY(10);
        button1.setLayoutX(10);

        Button button2 = new Button("xxxx");
        button2.setLayoutY(10);
        button2.setLayoutX(80);

        Pane pane = new Pane();
        StackPane sp = new StackPane();
        sp.setPrefHeight(400);
        sp.setPrefWidth(400);
        sp.setBorder(new Border(new BorderStroke(Color.RED, BorderStrokeStyle.SOLID, null, new BorderWidths(2))));

        HBox hBox1 = new HBox();
        hBox1.setAlignment(Pos.CENTER);
        hBox1.getChildren().add(new Button("IogyLoce"));
        hBox1.setBackground(new Background(new BackgroundFill(Color.CORNFLOWERBLUE, null, null)));

        HBox hBox2 = new HBox();
        hBox2.setAlignment(Pos.CENTER);
        hBox2.getChildren().add(new Button("IogyLoce"));
        hBox2.setBackground(new Background(new BackgroundFill(Color.ROSYBROWN, null, null)));
        hBox2.setBorder(new Border(new BorderStroke(Color.AQUA, BorderStrokeStyle.SOLID, null, new BorderWidths(2))));
        sp.getChildren().addAll(hBox1, hBox2);

        TranslateTransition tt = new TranslateTransition();
        tt.setDuration(Duration.seconds(3));
        tt.setNode(pane);
        tt.setFromX(0);
        tt.setToX(400);
//        tt.setAutoReverse(true);
        tt.setInterpolator(Interpolator.LINEAR);
        tt.setCycleCount(1);

        DisplacementMap dismap = new DisplacementMap();
//        dismap.setWrap(true);
//        dismap.setOffsetX(-0.5);
        hBox2.setEffect(dismap);

        DisplacementMap dismap2 = new  DisplacementMap();
        hBox2.setEffect(dismap2);
        pane.translateXProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observableValue, Number number, Number t1) {
//            hBox2.setTranslateX(t1.intValue());
//                dismap.setOffsetX(t1.doubleValue() / 400);
//                dismap.setOffsetY(t1.doubleValue() / 400);
                dismap.setOffsetX(t1.doubleValue() / 400);
                dismap.setOffsetY(t1.doubleValue() / 400);
                dismap2.setOffsetX(1-(t1.doubleValue() / 400));
            }
        });

        tt.setOnFinished(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
//                sp.getChildren().remove(hBox2);
//                hBox2.setBackground();
                System.out.println(dismap.getOffsetX());
                System.out.println(dismap2.getOffsetY());
            }
        });

        AnchorPane root = new AnchorPane();
        root.getChildren().addAll(button1,button2,sp);
        AnchorPane.setTopAnchor(sp, 200.0);
        AnchorPane.setLeftAnchor(sp, 200.0);

        button1.setOnMouseClicked(e->  tt.play());




        Scene scene = new Scene(root, 1500, 800);
        primaryStage.setTitle("Characters around circle");
        primaryStage.setScene(scene);

        //Display
        primaryStage.show();

    }

}